
package com.zhongketech.sunflower.utils;

import com.zhongketech.sunflower.MyApplication;
import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.util.LruCache;

@SuppressLint("NewApi")
public class ImageCache {
    final static String TAG = "ImageCache";

    private final LruCache<String, Object> mCache;

    public ImageCache(int maxSize) {
        mCache = new LruCache<String, Object>(maxSize) {
            @Override
            protected int sizeOf(String key, Object value) {
                return sizeOfCache(key, value);
            }

            @Override
            protected void entryRemoved(boolean evicted, String key,
                    Object oldValue, Object newValue) {
                entryRemovedCache(evicted, key, oldValue, newValue);
            }

            @Override
            protected Object create(String key) {
                return createCache(key);
            }
        };
    }

    int sizeOfCache(String key, Object value) {
        if (value instanceof Bitmap) {
            return ((Bitmap) value).getByteCount();
        }
        Drawable d = (Drawable) value;
        return d.getIntrinsicWidth() * d.getIntrinsicWidth() * 4;

    }

    void entryRemovedCache(boolean evicted, String key, Object oldValue,
            Object newValue) {
        Log.w(TAG, String.format("%s relase resource evicted %b key %s", TAG,
                evicted, key));
    }

    Object createCache(String key) {
        return null;
    }

    public Bitmap getResourceBitmap(int rid) {
        String key = getBKey(rid);
        Object obj = mCache.get(key);
        if (obj != null && obj instanceof Bitmap)
            return (Bitmap) obj;
        Resources res = MyApplication.getInstance().getApplicationContext().getResources();
        Bitmap bt = BitmapFactory.decodeResource(res, rid);
        if (bt != null) {
            mCache.put(key, bt);
        }
        return bt;
    }

    public Drawable getResourceDrawable(int rid) {
        String key = getBKey(rid);
        Object obj = mCache.get(key);
        if (obj != null && obj instanceof Drawable)
            return (Drawable) obj;
        Resources res = MyApplication.getInstance().getApplicationContext().getResources();
        Drawable d = res.getDrawable(rid);
        if (d != null) {
            mCache.put(key, d);
        }
        return d;
    }

    public Bitmap get(String key) {
        String k = getKey(key);
        Object obj = mCache.get(k);
        if (obj != null && obj instanceof Bitmap)
            return (Bitmap) obj;
        return null;
    }

    public void put(int rid, Bitmap bt) {
        mCache.put(getBKey(rid), bt);
    }

    public void put(String key, Bitmap bt) {
        mCache.put(getKey(key), bt);
    }

    public void clear() {
        mCache.evictAll();
    }

    String getBKey(int rid) {
        return "B" + String.valueOf(rid);
    }

    String getDKey(int rid) {
        return "D" + String.valueOf(rid);
    }

    String getKey(String key) {
        return "S" + key;
    }
}
